package com.gs.zsmanage.config.interceptor;

import com.gs.zsmanage.config.log.Tracer;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/9/11 0011
 */
public class TracerInterceptor extends HandlerInterceptorAdapter {
    /*在前端请求到达服务器时，应用容器在执行实际业务处理之前,
    *为这个前端请求分配一个全局唯一的调用链ID
    */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String traceId= UUID.randomUUID().toString();
        Tracer.setTraceId(traceId);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        response.addHeader("traceId",Tracer.getTraceId());
    }
}
